package servlets;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderList  {
    public static Order findOrder(Integer id){
        return (Order)orders.get(id);
    }

    public static Order[] getOrders()
    {
        List<Order> list = new ArrayList<Order>(orders.values());
        return list.toArray(new Order[list.size()]);
    }

    public static Integer addOrder(Order order){
        Integer id = orders.size() + 1;
        order.setId(id);

        orders.put(order.getId(),order);

        return id;
    }

    public static void saveToBase()
    {
        Order[] orders = getOrders();
        for(int i = 0; i < orders.length; i++)
        {
            try {
                DBProvider.insertRecordIntoTable("INSERT INTO Order_"
                        + "(OrderStateId, BeginDate, Priority, TotalSum) VALUES(" +
                        orders[i].getOrderStates() + ", " +
                        orders[i].getBeginDate() + ", " +
                        orders[i].getPriority() + ", " +
                        orders[i].getTotalSum() + ")");
            }catch (SQLException e) {

                System.out.println(e.getMessage());

            }
        }

    }

    private static Map orders = new HashMap();
}

/*package servlets;

import java.util.ArrayList;

public class OrderList  {
    public static Order takeOrder(){
        return orders.size() > 0 ? (Order)orders.get(orders.size() - 1) : null;
    }
    public static boolean putOrder(Order order){
        boolean result = false;
        
        orders.add(order);
        result = true;
        
        return result;
    }
    
    public static int elementCount(){
    	return orders.size();
    }
    
    private static ArrayList orders = new ArrayList();
}*/